package com.kingcore.director.resourcemaster.dao.impl;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.dao.impl.DirectorDaoJdbcImpl;
import com.kingcore.director.core.bean.TreeNodeBean;
import com.kingcore.director.resourcemaster.dao.FavoriteFilesDao;

public class FavoriteFilesDaoImpl extends DirectorDaoJdbcImpl
				implements FavoriteFilesDao {

	private final static Logger log = Logger.getLogger(FavoriteFilesDaoImpl.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void deleteByLocations(List<String> locationsList) throws SQLException{
		List<String> sqlList = new ArrayList<String>();
		for (String location : locationsList) {
			String sql="Delete from "+DirectorConstants.FMT_FavoriteFiles+" Where Location='"+location+"'";
			sqlList.add(sql);
		}
		log.debug("begin executeBatch.");
		this.executeBatch(sqlList);  //批量处理提高性能，避免死锁
	}

	private String Sql_GetMaxClassId="Select max(class_id)+1 From "+DirectorConstants.FMT_FavoriteFiles_Class;
	@Override
	public void insertTreeNode(TreeNodeBean treeNodeBean) throws SQLException {
		List<String> sqls = new ArrayList<String>();
		int classId=this.queryForInt(Sql_GetMaxClassId);
		int orderId = treeNodeBean.getOrderId();
		int pid = treeNodeBean.getPid();
		int orderIdNew = 0;
		if (orderId==-1) { //子节点
			orderIdNew = this.queryForInt("Select max(order_id) From "+DirectorConstants.FMT_FavoriteFiles_Class +
					" Where pid="+pid);
		} else if (orderId>0){ //同级节点
			sqls.add("Update "+DirectorConstants.FMT_FavoriteFiles_Class+" Set order_id=order_id+1 " +
					" Where pid="+pid+" And order_id>"+orderId);
			orderIdNew = orderId;
		}
		orderIdNew++;
		
		treeNodeBean.setClassId(classId);
		String sql = "Insert Into "+DirectorConstants.FMT_FavoriteFiles_Class +
					"(class_id,class_name,pid,level_id,is_leaf,is_builtin,order_id)"+
					" values('"+classId+"','"+treeNodeBean.getClassName()+"','"+treeNodeBean.getPid()+"','"
					+treeNodeBean.getLevelId()+"','"+treeNodeBean.getIsLeaf()+"','"+treeNodeBean.getIsBuiltin()+"','"+orderIdNew+"')";
		sqls.add(sql);
		//this.executeUpdate(sql);
		this.executeBatch(sqls);
	}

	String Sql_UpdateTreeNode = 
			new StringBuffer().append("Update ")
					.append(DirectorConstants.FMT_FavoriteFiles_Class )
					.append(" Set class_name=?")
					.append(" Where class_id=?")
					.toString();
	@Override
	public void updateTreeNode(TreeNodeBean treeNodeBean) throws SQLException {
		Object[] params = {treeNodeBean.getClassName(), treeNodeBean.getClassId()};
		int[] paramTypes = {Types.VARCHAR, Types.INTEGER};
		this.executeUpdate(Sql_UpdateTreeNode, params, paramTypes);
		
	}

	String Sql_DeleteTreeNodeById = 
			new StringBuffer().append("Delete From ")
						.append(DirectorConstants.FMT_FavoriteFiles_Class)
						.append(" Where class_id=?")
						.toString();
	String Sql_ResortAfterDelete = 
		new StringBuffer().append("Update ")
					.append(DirectorConstants.FMT_FavoriteFiles_Class)
					.append(" Set order_id=order_id-1")
					.append(" Where pid=?")
					.append("  And order_id>?")
					.toString();
	@Override
	public void deleteTreeNode(TreeNodeBean treeNodeBean) throws SQLException {
		//String sql = "";
		Object[] params = {treeNodeBean.getClassId()};
		int[] paramTypes = {Types.INTEGER};
		this.executeUpdate(Sql_DeleteTreeNodeById, params, paramTypes);

		Object[] params2 = {treeNodeBean.getPid(), treeNodeBean.getOrderId()};
		int[] paramTypes2 = {Types.INTEGER,Types.INTEGER};
		this.executeUpdate(Sql_ResortAfterDelete, params2, paramTypes2);
	}

	@Override
	public void updateOrderById(TreeNodeBean treeNodeBean, int orderIdBefore)
			throws SQLException {
		List<String> sqls = new ArrayList<String>();
		sqls.add("Update "+DirectorConstants.FMT_FavoriteFiles_Class+" Set order_id=" +orderIdBefore+
				" Where pid="+treeNodeBean.getPid()+" And order_id="+treeNodeBean.getOrderId());
		sqls.add("Update "+DirectorConstants.FMT_FavoriteFiles_Class+" Set order_id= "+treeNodeBean.getOrderId()+
				" Where class_id="+treeNodeBean.getClassId());
		this.executeBatch(sqls);
	}

	@Override
	public void setFiles2Class(List<String> fileUriList, int classId)
			throws SQLException {
		List<String> sqls = new ArrayList<String>();
		for (String location : fileUriList) {
			sqls.add("Update "+DirectorConstants.FMT_FavoriteFiles +" Set class_id=" +classId +
					  " Where Location='"+location+"'");
		}
		this.executeBatch(sqls);
	}
	
}


